/*
* Copyright 2016 YONYOU Corporation Ltd. All Rights Reserved.
*
* This software is published under the terms of the YONYOU Software
* License version 1.0, a copy of which has been included with this
* distribution in the LICENSE.txt file.
*
* @Project         marketing

* @Package         com.yonyou.marketing.message.push.provider.impl

* @Author          LuZhen

* @FileName        WebChatCustomMessage

* @Date            16/11/10

----------------------------------------------------------------------------------
*     Date          Who             Version         Comments
* 1. 16/11/10        LuZhen          1.0             Create Source
*
----------------------------------------------------------------------------------
*/
package com.yonyou.gmmc.service.wechatpush.spi.webchattools;


import me.chanjar.weixin.mp.bean.template.WxMpTemplateData;
import me.chanjar.weixin.mp.bean.template.WxMpTemplateMessage;
import org.marketing.common.entity.wechatpushmessage.*;

import com.yonyou.gmmc.service.wechatpush.provider.impl.AbstractProviderMessage;

import static org.marketing.common.constants.MessagePushConstant.WX_MSG_PROP_URL;
import static org.marketing.common.constants.MessagePushConstant.WX_MSG_PROP_COLOR;



/**
 * 微信公众号模板消息
 */
public class WebChatTemplateMessage extends AbstractProviderMessage {

    public WxMpTemplateMessage getMessage() {
        return message;
    }

    private WxMpTemplateMessage message = WxMpTemplateMessage.builder().build();

    @Override
    public void transform() {
        WeChatMessageProtocol origin = pushMessage.getOrigin();
        message.setToUser(origin.getReceiver().getDetailList().get(0));
        message.setTemplateId(origin.getMessage().getContent());
//        message.setClientAppId(origin.getSender().getClientAppId());
        if(origin.getMessage().getExtraParaMap().containsKey(WX_MSG_PROP_URL)){
            message.setUrl(origin.getMessage().getExtraParaMap().get(WX_MSG_PROP_URL));
        }
//        if(origin.getMessage().getExtraParaMap().containsKey(WX_MSG_PROP_COLOR)){
//            message.(origin.getMessage().getExtraParaMap().get(WX_MSG_PROP_COLOR));
//        }
        for (WeChatMessageProtocolMessageTemplateContent templateData: origin.getMessage().getTemplateConentList()) {
            message.addWxMpTemplateData(new WxMpTemplateData(templateData.getKey(),templateData.getValue(),templateData.getExtraMap().get(WX_MSG_PROP_COLOR)));
        }
    }

    @Override
    public String toString() {
        return "WebChatTemplateMessage{" +
                "message=" + message +
                '}';
    }
}
